package com.ics.top.batch.management.listener;


import com.alibaba.fastjson.JSON;
import com.ics.top.batch.management.domain.BatchUserManagementDTO;
import com.ics.top.batch.management.service.BatchUserManagementLogClient;
import com.ics.top.domain.BatchUserManagementLogs;
import com.ics.top.second.domain.UserManagement;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.core.listener.ItemListenerSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by Administrator on 2018/4/24.
 */
@Component
public class UserManagementLoggerListener extends ItemListenerSupport<BatchUserManagementDTO, UserManagement>  implements StepExecutionListener  {
    private static final Logger log = LoggerFactory.getLogger(UserManagementLoggerListener.class);
    private Set<Long> ids = new HashSet<Long>();
    private List<UserManagement> list = new ArrayList<>();
    @Autowired
    private BatchUserManagementLogClient batchUserManagementLogClient;
    @Override
    public void afterRead(BatchUserManagementDTO item) {
        ids.add(item.getId());
        log.info("batch afterRead");
        super.afterRead(item);
    }

    @Override
    public void beforeRead() {
        log.info("batch beforeRead");
        super.beforeRead();
    }

    @Override
    public void onReadError(Exception ex) {
        log.info("batch onReadError");
        super.onReadError(ex);
    }

    @Override
    public void afterProcess(BatchUserManagementDTO item, UserManagement result) {
        log.info("batch afterProcess");
        super.afterProcess(item, result);
    }

    @Override
    public void beforeProcess(BatchUserManagementDTO item) {
        log.info("batch beforeProcess");

        super.beforeProcess(item);
    }

    @Override
    public void onProcessError(BatchUserManagementDTO item, Exception e) {
        log.info("batch onProcessError");
        super.onProcessError(item, e);
    }

    @Override
    public void afterWrite(List<? extends UserManagement> item) {
        log.info("batch afterWrite");
        log.info("数量："+item.size());
        super.afterWrite(item);
    }

    @Override
    public void beforeWrite(List<? extends UserManagement> item) {
        list.addAll(item);
        super.beforeWrite(item);
    }

    @Override
    public void onWriteError(Exception ex, List<? extends UserManagement> item) {
        log.info("batch onWriteError");
        super.onWriteError(ex, item);
    }

    @Override
    public void beforeStep(StepExecution stepExecution) {
        log.info("batch beforeStep");

    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        log.info("batch afterStep");
        Long id = 0L;
        log.info("保存数据 List<AssessData>  {} ",list);
        if(list.size() > 0){
            String  callPlanIdsString  = ","+ StringUtils.join(ids.toArray(), ",")+",";
            BatchUserManagementLogs batchLogs = batchUserManagementLogClient.add( list.size() ,callPlanIdsString , JSON.toJSONString(list) ,"NEW_GENERATION");
            if(batchLogs != null){
                id = batchLogs.getId();
            }
        }
        list.clear();
        ids.clear();
        return new ExitStatus(id+"" );
    }
}
